<template>
  <el-form
    :model="ruleForm"
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
    style="width: 600px; margin: 200px auto"
  >
    <el-form-item label="等级名称" prop="levelName">
      <el-input v-model="ruleForm.levelName"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { saveOrUpdateLevel } from "@/api/level";
export default {
  data() {
    return {
      ruleForm: {
        levelName: "",
      },
      rules: {
        levelName: [
          { required: true, message: "请输入活动名称", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //新增等级
          saveOrUpdateLevel({
            levelname: this.ruleForm.levelName,
          })
            .then((res) => {
              if (res.code == 200) {
                this.$router.push("/level/levelList");
                this.$message({
                  type: "success",
                  message: "成功添加",
                });
              }
              // console.log(this.ruleForm.levelName);
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>

<style>
</style>